package com.shop.filter; /**
 * ${PACKAGE_NAME}文档说明
 *
 * @author Angluzha
 * @version 2022/9/17
 */

import com.shop.entity.User;
import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebFilter(filterName = "Filter_02_Login" ,value = "/*")
public class Filter_02_Login implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        String uri = req.getRequestURI();
        if (req.getSession().getAttribute("user")!=null){
            chain.doFilter(request, response);
        }else if (uri.toLowerCase().contains("login")){//登录放行
            chain.doFilter(request, response);
        }else if (uri.toLowerCase().contains("register")){//注册放行
            chain.doFilter(request, response);
        }else if (uri.toLowerCase().contains("save")){//注册放行
            chain.doFilter(request, response);
        }else if (uri.toLowerCase().contains("index")){//首页放行
            chain.doFilter(request, response);
        }else if (uri.toLowerCase().contains("image")){//图片放行
            chain.doFilter(request, response);
        }else {
            resp.sendRedirect("/login.jsp");
        }
    }
}
